//
//  AcceptDetailVC.m
//  Zhiche-Carriage
//
//  Created by 王亚陆 on 16/6/15.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "AcceptDetailVC.h"
#import <Masonry.h>
#import "CarShipCell.h"
#import "Common.h"
#import "QuoteCell.h"
#import "CarCell.h"
#import <QuartzCore/QuartzCore.h>
#import "AddressCell1.h"
#import "BequoteVC.h"
#define TimeTag 100
#define TableTag  200
#define SmallTabelTag 300
#define chooseTag 400

@interface AcceptDetailVC ()
{
    UIImageView * nav;
    UIScrollView * scroll;
    UITableView * table;
    UITableView * CarTabel;
    NSMutableDictionary * dataSorceDic;
    Common * Com;
}

@property (nonatomic,strong) UIView * BackView;

@end

@implementation AcceptDetailVC
- (instancetype)init

{
    self = [super init];
    if (self) {
        self.ID = [[NSString alloc]init];
    }
    return self;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =WhiteColor;
    dataSorceDic = [[NSMutableDictionary alloc]init];
    
    Com = [[Common alloc]init];
    nav = [self createNav:@"运单详情-未报价"];

    [self.view addSubview:nav];
    [self createScroll];
    [self createTable];
}


-(void)viewWillAppear:(BOOL)animated{
    

    
    NSString * string = [NSString stringWithFormat:@"%@/%@",order_Url,self.ID];
    
    [Common requestWithUrlString:string contentType:@"application/json" finished:^(id responseObj){
        
//        data =     {
//            actualCost = "<null>";
//            arriveDate = "2016-06-15";
//            comment = "<null>";
//            deliveryDate = "2016-06-15";
//            departCityName = "\U6c88\U9633\U5e02";
//            departUnit = "\U8fbd\U5b81\U6c88\U9633\U5e93";
//            depositCost = "<null>";
//            id = 436;
//            needPayCost = "<null>";
//            orderCode = CO20160615104508800;
//            paidCost = "<null>";
//            payStatus = 10;
//            payStatusText = "<null>";
//            postTimeRemaining = "<null>";
//            receiptCityName = "\U5510\U5c71\U5e02";
//            receiptUnit = "\U5510\U5c71\U5e02\U4e2d\U5fc3\U5e93";
//            status = 20;
//            statusText = "<null>";
//            unpaidCost = "<null>";
//            vehicles =         (
//                                {
//                                    amount = 1;
//                                    brandName = "\U91d1\U676f";
//                                    id = 588;
//                                    vehicleName = "\U5927\U529b\U795e";
//                                },
//                                {
//                                    amount = 1;
//                                    brandName = "\U5965\U8fea";
//                                    id = 589;
//                                    vehicleName = "\U5965\U8feaA1";
//                                }
//                                );
//        };
//        message = "\U67e5\U8be2\U6210\U529f\Uff01";
//        messageCode = "<null>";
//        success = 1;
//    }

        dataSorceDic = responseObj[@"data"];
        NSLog(@"%@",responseObj);
    [self createUI:dataSorceDic];
        [CarTabel reloadData];
        [table reloadData];

    
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];

}
-(void)createUI:(NSMutableDictionary *)AddressDic{
    
    NSString * ordercode = [NSString stringWithFormat:@"%@",AddressDic[@"orderCode"]];
    UILabel * label = [self createUIlabel:ordercode andFont:FontOfSize14 andColor:BlackColor];
    [_BackView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(_BackView.mas_top).with.offset(10*kHeight);
    }];
    
    UILabel * CarModel = [self createUIlabel:@"地址" andFont:14.0 andColor:RGBACOLOR(149, 149, 149, 1)];
    [_BackView addSubview:CarModel];
    [CarModel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(label.mas_bottom).with.offset(10*kHeight);
    }];
    [table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(0);
        make.top.mas_equalTo(CarModel.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 200*kHeight));
    }];
    
    UILabel * timelabel = [self createUIlabel:@"时间" andFont:14.0 andColor:RGBACOLOR(149, 149, 149, 1)];
    [_BackView addSubview:timelabel];
    [timelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(table.mas_bottom).with.offset(10*kHeight);
    }];
    NSMutableArray * arr = [[NSMutableArray alloc]init];
    NSString * str1 = [NSString stringWithFormat:@"%@     %@",AddressDic[@"deliveryDate"],AddressDic[@"deliverytime"]] ;
    [arr addObject:str1];
    
    NSString * str2 = [NSString stringWithFormat:@"%@     %@",AddressDic[@"arriveDate"],AddressDic[@"arrivetime"]] ;
    [arr addObject:str2];
    
    NSArray * arrTime = @[@"提车时间:",@"送达时间:"];
    for (int i=0; i<arrTime.count; i++) {
        UILabel * time = [self createUIlabel:arrTime[i] andFont:FontOfSize14 andColor:BlackColor];
        [_BackView addSubview:time];
        [time mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
            make.top.equalTo(timelabel.mas_bottom).with.offset(10*kHeight+36*kHeight*i);
        }];
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        
        [btn setTitleColor:BlackColor forState:UIControlStateNormal];
        btn.tag = TimeTag+i;
        btn.titleLabel.font = Font(FontOfSize14);
        [_BackView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_BackView.mas_centerX);
            make.centerY.equalTo(time.mas_centerY);
        }];
        UIImageView * imageline =[[UIImageView alloc]init];
        imageline.image = [UIImage imageNamed:@"common_list_line1"];
        
        if(i==1){
            imageline.tag = 50;
        }
        [_BackView addSubview:imageline];
        [imageline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_BackView.mas_left).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
            make.top.mas_equalTo(time.mas_bottom).with.offset(10*kHeight);
        }];
    }
    
    UIImageView * imageLine = (UIImageView*)[_BackView viewWithTag:50];
    UILabel * carModel = [self createUIlabel:@"车型" andFont:14.0 andColor:RGBACOLOR(149, 149, 149, 1)];
    [_BackView addSubview:carModel];
    [carModel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(imageLine.mas_bottom).with.offset(10*kHeight);
    }];
    NSInteger number = [AddressDic[@"vehicles"] count];

    NSString * Number = [NSString stringWithFormat:@"总价:%ld台",number];
    UILabel * amount =[self createUIlabel:Number andFont:FontOfSize14 andColor: BlackColor];
    [_BackView addSubview:amount];
    [amount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_BackView.mas_right).with.offset(-14*kWidth);
        make.centerX.mas_equalTo(carModel.mas_centerX);
    }];

    
    CarTabel.backgroundColor = [UIColor cyanColor];
    [CarTabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_BackView.mas_left).with.offset(0);
        make.top.mas_equalTo(carModel.mas_bottom).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 45*(number)*kHeight));
    }];
    
    UILabel * comment = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100*kWidth, 30*kHeight)];
    comment.text = @"备注";
    comment.font = Font(12);
    comment.textColor = RGBACOLOR(149, 149, 149, 1);
    [_BackView addSubview:comment];
    [comment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(CarTabel.mas_bottom).with.offset(5*kHeight);
        make.size.mas_equalTo(CGSizeMake(200, 25));
    }];
    
    NSString * commentLabel = [NSString stringWithFormat:@"%@",AddressDic[@"comment"]];
    UILabel * field =[self createUIlabel:commentLabel andFont:FontOfSize14 andColor:BlackColor];
    
    [_BackView addSubview:field];
    [field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(comment.mas_bottom).with.offset(5*kHeight);
    }];
    
    UIView * Hline1 =[[UIView alloc]init];
    Hline1.backgroundColor = RGBACOLOR(149, 149, 149, 1);
    [_BackView addSubview:Hline1];
    [Hline1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_BackView.mas_centerX);
        make.top.mas_equalTo(field.mas_bottom).with.offset(5*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
    }];
    
    UIButton * ShipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [ShipBtn setTitle:@"我要报价" forState:UIControlStateNormal];
    [ShipBtn setTitleColor:BlackColor forState:UIControlStateNormal];
    [ShipBtn addTarget:self action:@selector(pressShipBtn) forControlEvents:UIControlEventTouchUpInside];
    ShipBtn.layer.borderWidth = 1;
    ShipBtn.layer.borderColor = RGBACOLOR(149, 149, 149, 1).CGColor;
    ShipBtn.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:ShipBtn];
    [ShipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(Main_Width/3*2, 35*kHeight));
        make.top.mas_equalTo(Hline1.mas_bottom).with.offset(30*kHeight);
    }];
}
-(void)pressShipBtn{
    
    BequoteVC * bequote = [[BequoteVC alloc]init];
    bequote.orderid = self.ID;
    [self.navigationController pushViewController:bequote animated:YES];
    
}
-(void)createScroll{
    
    scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, Main_Width,Main_height-64*kHeight)];
    scroll.contentOffset = CGPointMake(0, 0);
    scroll.contentSize = CGSizeMake(Main_Width, Main_height*1.5);
    self.automaticallyAdjustsScrollViewInsets =NO;
    scroll.bounces = NO;
    //隐藏横向、纵向的滚动条
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    scroll.delegate = self;
    scroll.minimumZoomScale = 1.0;
    scroll.maximumZoomScale = 3.0;
    [self.view addSubview:scroll];
    
    [scroll mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(nav.mas_bottom).with.offset(0*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, Main_height-64*kHeight));
    }];
    //设置scrollView的内容视图
    _BackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, Main_height*kHeight*1.5)];
    _BackView.backgroundColor = [UIColor whiteColor];
    [scroll addSubview:_BackView];
}

-(void)createTable{
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Main_Width, Main_height) style:UITableViewStylePlain];
    [table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    table.userInteractionEnabled = NO;
    table.tag = TableTag;
    table.delegate = self;
    table.dataSource = self;
    [_BackView addSubview:table];
    
    CarTabel = [[UITableView alloc]initWithFrame:CGRectMake(0, 200, Main_Width, Main_height) style:UITableViewStylePlain];
    [CarTabel setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    CarTabel.userInteractionEnabled = NO;
    CarTabel.tag = SmallTabelTag;
    CarTabel.delegate = self;
    CarTabel.dataSource = self;
    [_BackView addSubview:CarTabel];
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *str=@"str";
    
    if (tableView.tag== TableTag) {
        AddressCell1 * cell =  [tableView dequeueReusableCellWithIdentifier:str];
        if (cell ==nil) {
            cell = [[AddressCell1 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row ==0) {
            
            cell.CityName.text = [NSString stringWithFormat:@"%@",dataSorceDic[@"departCityName"]];
           
            UIImageView * imageline =[[UIImageView alloc]init];
            imageline.image = [UIImage imageNamed:@"common_list_line1"];
            [cell.contentView addSubview:imageline];
            [imageline mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(cell.contentView).with.offset(0);
                make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
                make.bottom.mas_equalTo(cell.contentView.mas_bottom);
            }];
        }
        else{
            
            cell.CityName.text = [NSString stringWithFormat:@"%@",dataSorceDic[@"receiptCityName"]];
            UIView * Hline =[[UIView alloc]init];
            Hline.backgroundColor = RGBACOLOR(149, 149, 149, 1);
            [cell.contentView addSubview:Hline];
            [Hline mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell.contentView.mas_left);
                make.bottom.mas_equalTo(cell.contentView.mas_bottom);
                make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
            }];
        }
        return cell;
    }
    else{
        CarShipCell * cell =  [tableView dequeueReusableCellWithIdentifier:str];
        if (cell ==nil) {
            cell = [[CarShipCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        cell.CarName.text = [NSString stringWithFormat:@"%@",dataSorceDic[@"vehicles"][indexPath.row][@"brandName"]];
        cell.CarNumber.text = [NSString stringWithFormat:@"%@",dataSorceDic[@"vehicles"][indexPath.row][@"amount"]];
        
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return (tableView.tag == TableTag?100*kHeight:37*kHeight);
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView.tag == TableTag) {
        
        return 2;
    }
    else{
        return [dataSorceDic[@"vehicles"] count];
    }
}


-(UILabel*)createUIlabel:(NSString *)title andFont:(CGFloat)font andColor:(UIColor*)color {
    UILabel * label =[[UILabel alloc]init];
    label.text = title;
    label.textColor = color;
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:font];CGSize size = [label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
    CGFloat nameH = size.height;
    CGFloat nameW = size.width;
    label.textAlignment = NSTextAlignmentLeft;
    label.frame =CGRectMake(0, 0, nameW, nameH);
    label.font = Font(font);
    return label;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
